package com.ljx.funchat.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ljx.funchat.chat.dao.RoomGroupDao;
import com.ljx.funchat.chat.dao.RoomGroupMemberDao;
import com.ljx.funchat.chat.domain.entity.RoomGroup;
import com.ljx.funchat.chat.service.IRoomService;
import com.ljx.funchat.chat.service.adapter.RoomAdapter;
import com.ljx.funchat.common.domain.vo.req.CursorPageBaseReq;
import com.ljx.funchat.common.domain.vo.resp.CursorPageBaseResp;
import com.ljx.funchat.user.dao.UserDao;
import com.ljx.funchat.user.dao.UserFriendDao;
import com.ljx.funchat.user.domain.entity.User;
import com.ljx.funchat.user.domain.entity.UserFriend;
import com.ljx.funchat.user.domain.vo.req.UserFriendDeleteReq;
import com.ljx.funchat.user.domain.vo.req.UserFriendSearchReq;
import com.ljx.funchat.user.domain.vo.resp.UserFriendResp;
import com.ljx.funchat.user.domain.vo.resp.UserFriendSearchResp;
import com.ljx.funchat.user.service.IUserFriendService;
import com.ljx.funchat.user.service.adatper.UserFriendAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class UserFriendServiceImpl implements IUserFriendService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserFriendDao userFriendDao;

    @Autowired
    private RoomGroupDao roomGroupDao;

    @Autowired
    private RoomGroupMemberDao roomGroupMemberDao;

    @Autowired
    private IRoomService roomService;

    @Override
    public CursorPageBaseResp<UserFriendResp> friendPage(Long uid, CursorPageBaseReq request) {
        // 获取分页
        CursorPageBaseResp<UserFriend> resp = userFriendDao.getFriendPage(uid, request);
        // 获取好友信息Map
        List<Long> friendIds = resp.getList().stream().map(UserFriend::getFriendUid).collect(Collectors.toList());
        List<User> userList = userDao.getByFriendIds(friendIds);
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        // 转为resp返回
        List<UserFriendResp> userFriendRespList = UserFriendAdapter.buildUserFriendResp(resp.getList(), userMap);
        return CursorPageBaseResp.init(resp, userFriendRespList);
    }

    @Override
    public UserFriendSearchResp search(Long uid, UserFriendSearchReq req) {
        UserFriendSearchResp res = new UserFriendSearchResp();
        Long targetId = req.getTargetId();
        // 搜索好友
        User user = userDao.getById(targetId);
        if (Objects.nonNull(user)) {
            boolean userFlag = userFriendDao.countIsFriend(uid, targetId);
            res.setUserInfo(UserFriendAdapter.buildSearch(user, userFlag));
        } else {
            res.setUserInfo(null);
        }
        // 搜索群聊
        RoomGroup roomGroup = roomGroupDao.getById(targetId);
        if (Objects.nonNull(roomGroup)) {
            boolean roomFlag = roomGroupMemberDao.countIsJoin(uid, targetId);
            res.setGroupInfo(RoomAdapter.buildSearch(roomGroup, roomFlag));
        } else {
            res.setGroupInfo(null);
        }

        return res;
    }

    @Override
    @Transactional
    public void deleteFriend(Long uid, UserFriendDeleteReq req) {
        // 查询是否有好友关系
        List<UserFriend> userFriends = userFriendDao.getFriendByIds(uid, req.getTargetId());
        if (CollectionUtil.isEmpty(userFriends)){
            return;
        }
        // 删除好友
        userFriendDao.deleteByIds(uid, req.getTargetId());
        // 删除好友房间
        String key = getRoomFriendKey(uid, req.getTargetId());
//        RoomFriend roomFriend = roomFriendDao.getByKey(key);
        roomService.disableRoomFriendByKey(key);
        // 删除会话
//        contactService.deleteContact(Arrays.asList(uid, req.getTargetId()), roomFriend.getRoomId());
    }

    /**
     * 获取好友房间的key
     *
     * @param uid
     * @param targetId
     * @return
     */
    private String getRoomFriendKey(Long uid, Long targetId) {
        return uid > targetId ? targetId + "_" + uid : uid + "_" + targetId;
    }
}
